package top.lunarye.modules.staff.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.lunarye.modules.staff.entity.Role;
import top.lunarye.modules.staff.entity.RoleEnum;
import top.lunarye.modules.staff.entity.dto.RoleDTO;
import top.lunarye.modules.staff.service.impl.RoleServiceImpl;
import top.lunarye.utils.RestResponse;
import top.lunarye.utils.SystemCode;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author lunar
 * @since 2021-07-13
 */
@Api(tags = "角色模块")
@RestController
@SaCheckLogin
@RequestMapping("/user/role")
public class RoleController {

    @Autowired
    RoleServiceImpl roleService;

    @PostMapping
    @ApiOperation("添加角色")
    @SaCheckRole(value = {"teacher", "admin"},mode = SaMode.OR)
    public RestResponse addRole(@RequestParam("roleName") String roleName) {
        Role insertRole = new Role();
        insertRole.setRoleName(roleName);
        boolean result = roleService.save(insertRole);
        if (result) {
            return RestResponse.ok();
        } else {
            return RestResponse.fail(SystemCode.InnerError.getCode(), "角色插入失败");
        }
    }

    @GetMapping(value="/{id}")
    @ApiOperation("根据id查询角色")
    @ApiImplicitParam(name = "id", value="角色id", required = true, dataType = "int")
    public RestResponse<Role> getRole(@PathVariable("id") Integer id) {
        try {
            Role role = roleService.getById(id);
            if (role ==  null) {
                return RestResponse.fail(SystemCode.InnerError.getCode(), "角色获取失败");
            }
            return RestResponse.ok(role);
        } catch (Exception e) {
            return RestResponse.fail(SystemCode.InnerError.getCode(), SystemCode.InnerError.getMessage());
        }
    }

    @GetMapping
    @ApiOperation("获取角色列表")
    public RestResponse<List<RoleDTO>> getAllRole() {
        try {
            List<Role> list = roleService.list();
            if (list.isEmpty()) {
                return RestResponse.fail(SystemCode.InnerError.getCode(), "角色列表获取失败");
            }
            List<RoleDTO> collect = list.stream().map(item -> {
                RoleDTO roleDTO = new RoleDTO();
                String role = RoleEnum.getRole(item.getId());
                BeanUtils.copyProperties(item, roleDTO);
                roleDTO.setKey(role);
                return roleDTO;
            }).collect(Collectors.toList());
            return RestResponse.ok(collect);
        } catch (Exception e) {
            return RestResponse.fail(SystemCode.InnerError.getCode(), SystemCode.InnerError.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @ApiImplicitParam(name = "id", value="角色id", required = true, dataType = "int")
    @SaCheckRole(value = {"teacher", "admin"},mode = SaMode.OR)
    @ApiOperation("删除角色")
    public RestResponse deleteRole(@PathVariable("id") Integer id) {
        try {
            boolean result = roleService.removeById(id);
            if (!result) {
                return RestResponse.fail(SystemCode.InnerError.getCode(), "角色删除失败");
            }
            return RestResponse.ok();
        } catch (Exception e) {
            return RestResponse.fail(SystemCode.InnerError.getCode(), SystemCode.InnerError.getMessage());
        }
    }

    @PutMapping
    @ApiImplicitParam(name = "id", value="角色id", required = true, dataType = "int")
    @SaCheckRole(value = {"teacher", "admin"},mode = SaMode.OR)
    @ApiOperation("更新角色信息")
    public RestResponse updateRole(@RequestBody RoleDTO role) {
        try {
            Role updateRole = new Role();
            BeanUtils.copyProperties(role, updateRole);
            boolean result = roleService.updateById(updateRole);
            if (!result) {
                return RestResponse.fail(SystemCode.InnerError.getCode(), "角色更新失败");
            }
            return RestResponse.ok();
        } catch (BeansException e) {
            return RestResponse.fail(SystemCode.InnerError.getCode(), SystemCode.InnerError.getMessage());
        }
    }

    @GetMapping("/{current}/{size}")
    @ApiOperation("分页查询角色信息")
    public RestResponse<List<Role>> getRoleByPage(@PathVariable("current") Integer current, @PathVariable("size") Integer size) {
        try {
            Page<Role> page = new Page<>(current, size);
            List<Role> roles = roleService.selectAndPage(page, null);
            if (roles.isEmpty()) {
                return RestResponse.fail(SystemCode.InnerError.getCode(), "角色分页查询失败");
            }
            return RestResponse.ok(roles);
        } catch (Exception e) {
            return RestResponse.fail(SystemCode.InnerError.getCode(), SystemCode.InnerError.getMessage());
        }
    }

}

